using System.Collections.Generic;
using System.Linq;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;

namespace BITS.PreCompiler.CSharp.Syntax.Extensions
{
    /// <summary>
    /// Extension methods for class nodes
    /// </summary>
    public static partial class ClassExtension
    {
        /// <summary>
        /// Creates a class node.
        /// </summary>
        /// <param name="classNode">The class node.</param>
        /// <returns></returns>
        public static ClassDeclarationSyntax CreateEmptyClass(this ClassDeclarationSyntax classNode)
        {
            return Roslyn.Compilers.CSharp.Syntax.ClassDeclaration(classNode.Identifier)
                .AddModifiers(classNode.Modifiers.ToArray())
                .AddTypeParameterListParameters(classNode.TypeParameterList.Parameters.ToArray())
                .AddConstraintClauses(classNode.ConstraintClauses.ToArray());
        }

        /// <summary>
        /// Replaces the method.
        /// </summary>
        /// <param name="classNode">The class node.</param>
        /// <param name="oldMethod">The old method.</param>
        /// <param name="newMethod">The new method.</param>
        /// <returns></returns>
        public static ClassDeclarationSyntax ReplaceMethod(this ClassDeclarationSyntax classNode, MethodDeclarationSyntax oldMethod, MethodDeclarationSyntax newMethod)
        {
            return classNode.ReplaceNode(oldMethod, newMethod);
        }

        /// <summary>
        /// Gets method nodes.
        /// </summary>
        /// <param name="classNode">The class node.</param>
        /// <returns></returns>
        public static IEnumerable<MethodDeclarationSyntax> GetMethods(this ClassDeclarationSyntax classNode)
        {
            return classNode.ChildNodes().OfType<MethodDeclarationSyntax>();
        }
    }
}